#include "AIActor.h"
#include "AIPackage.h"

namespace ai
{
	void AIActor::setUserData(void* value)
	{
		_userData = value;
	}

	void* AIActor::getUserData() const
	{
		return _userData;
	}

	void AIActor::setMemoryValue(unsigned int key, const AIValue& value)
	{
		_memory[key] = value;
	}

	const AIValue& AIActor::getMemoryValue(unsigned int key) const
	{
		auto it = _memory.find(key);
		if (it == _memory.end())
			return AIValue::Null;
		else
			return it->second;
	}

	void AIActor::addPackage( AIPackage* package )
	{
		_packages.push_back(package);
	}

	AIActor::AIActor() : _activePackage(nullptr)
	{

	}

	void AIActor::update()
	{
		if (_activePackage == nullptr)
		{
			selectActivePackage();
		}

		if (_activePackage != nullptr)
		{
			_activePackage->run(this);
		}
	}

	void AIActor::selectActivePackage()
	{
		for (auto it = _packages.begin(); it != _packages.end(); ++it)
		{
			AIPackage* package = *it;
			if (package->checkCondition(this))
			{
				_activePackage = package;
				break;
			}
		}
	}

}